/*
 * Copyright (C) 2016 The Android Open Source Project
 *  Created by  Ant  2016/8/22  12:00
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ant.my.camera.activities;

import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Button;

import com.ant.my.camera.R;
import com.ant.my.camera.base.BaseActivity;

import java.io.IOException;

/**
 * Created by baichuan on 2016/8/22.
 */
public class MyCameraActivity extends BaseActivity implements SurfaceHolder.Callback {

    private SurfaceView msv;
    private SurfaceHolder holder;
    private Button obt;

    private Camera camera;

    @Override
    public int getContentLayout() {
        return R.layout.ac_camera;
    }

    @Override
    public void initView() {
        msv = (SurfaceView) findViewById(R.id.ac_mycamera_surfaceview);
        obt = (Button) findViewById(R.id.ac_mycamera_capture);
    }

    @Override
    public void initData() {

    }


    private void getCamera() throws IOException {
        if (camera == null) {
            camera = Camera.open();
            holder = msv.getHolder();
            holder.addCallback(this);
            camera.setPreviewDisplay(holder);
            camera.setDisplayOrientation(90);
            camera.startPreview();
        }
    }


    private void relase() {
        try {
            if (camera != null) {
                camera.setPreviewDisplay(null);
                camera.release();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initListener() {

    }

    /******************************************************************/
    @Override
    protected void onStart() {
        super.onStart();
        try {
            getCamera();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        relase();
    }

    /******************************************************************/

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {

    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

    }
}
